package pers.echo.tester.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    static String[] tables = new String[]{"TEST"};



    public static DataSourceConfig getOracleDataSrouceConfig(){
        String JDBC_URL = "jdbc:oracle:thin:@192.168.0.100:1521/orcl";
        String JDBC_USER = "jxgtjy";
        String JDBC_PASS = "jxgtjy";
        String Driver = "oracle.jdbc.driver.OracleDriver";
        DataSourceConfig dsc = getDataSrouceConfig(JDBC_URL, JDBC_USER, JDBC_PASS, Driver);
        return dsc;
    }
    public static DataSourceConfig getMysqlDataSrouceConfig(){
        String JDBC_URL = "jdbc:mysql://localhost:3306/learnjdbc?useUnicode=true&useSSL=false&characterEncoding=utf8";
        String JDBC_USER = "root";
        String JDBC_PASS = "root";
        String Driver = "com.mysql.jdbc.Driver";
        DataSourceConfig dsc = getDataSrouceConfig(JDBC_URL, JDBC_USER, JDBC_PASS, Driver);
        return dsc;
    }

    public static DataSourceConfig getDataSrouceConfig(String jdbc_url, String jdbc_user,
                                                       String jdbc_pass, String driverName){
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName(driverName);
        dsc.setUrl(jdbc_url);
        dsc.setUsername(jdbc_user);
        dsc.setPassword(jdbc_pass);
        return dsc;
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("jobob");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        mpg.setDataSource(getOracleDataSrouceConfig());

        // 包配置
        final PackageConfig pc = new PackageConfig();
        pc.setParent("pers.echo.tester");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        //strategy.setRestControllerStyle(true);


        strategy.setInclude(tables);
        //驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);

        mpg.execute();
    }

}
